package com.example.BOOK.view;

import static java.lang.Math.random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.NonNull;

import com.example.BOOK.R;
import com.example.BOOK.data.CirleSpriter;

import java.util.ArrayList;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder surfaceHolder;


    public GameView(Context context) {
        super(context);
        initView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        initView();
    }

    private void initView() {
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);
    }

    private DrawThread drawThread;
    private boolean isTouched =false;
    private float touchedx=-1;
    private float touchedy=-1;
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        if(null==drawThread){
            drawThread = new DrawThread();
            drawThread.start();

        }

        GameView.this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(MotionEvent.ACTION_DOWN==motionEvent.getAction()){
                    touchedx=motionEvent.getX();
                    touchedy=motionEvent.getY();
                    isTouched=true;

                }
                return false;
            }
        });

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(
            @NonNull SurfaceHolder surfaceHolder) {
        if (null != drawThread)
            drawThread.mystop();
        try {
            drawThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class DrawThread extends Thread{
        private ArrayList<CirleSpriter> spriters=new ArrayList<>();
        private boolean isStopped=false;
        public DrawThread(){
            Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.jinan1);
            spriters.add(new CirleSpriter(0,0,120,GameView.this.getWidth(),GameView.this.getHeight(),myBitmap));
            spriters.add(new CirleSpriter(240,240,120,GameView.this.getWidth(),GameView.this.getHeight(),myBitmap));
            spriters.add(new CirleSpriter(360,360,120,GameView.this.getWidth(),GameView.this.getHeight(),myBitmap));
            spriters.add(new CirleSpriter(480,480,120,GameView.this.getWidth(),GameView.this.getHeight(),myBitmap));
        }
        public void mystop()
        {
            isStopped=true;
        }

        @Override
        public void run() {
            super.run();
            Canvas canvas = null;
            int core=0;
            while (!isStopped) {
                try {

                    canvas = surfaceHolder.lockCanvas();

                    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
                    Matrix matrix = new Matrix();
                    matrix.postScale(2.0f, 1.5f);
                    canvas.drawBitmap(myBitmap, matrix, null);

                    Paint paint1=new Paint();
                    paint1.setColor(Color.WHITE);
                    if(GameView.this.isTouched){
                        for(int index=0;index<spriters.size();index++){
                            if(spriters.get(index).isShoted(GameView.this.touchedx,GameView.this.touchedy)){
                                canvas.drawBitmap(myBitmap,matrix,null);
                                core++;
                            }

                        }
                    }
                    for(int index=0;index<spriters.size();index++){
                        spriters.get(index).draw(canvas);
                    }
                    for(int index=0;index<spriters.size();index++){
                        spriters.get(index).move();
                    }
                    GameView.this.isTouched=false;
                    Paint paint=new Paint();
                    paint.setColor(Color.WHITE);
                    paint.setTextSize(50);
                    canvas.drawText("得分"+(core+1),10,100,paint);
                } catch (Exception a) {
                } finally {
                    if (null !=canvas) {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
